#coding:utf8
from defaultChi import *
from myBuf import *
        
class dam():
    def __init__(self):
        if not self.load():
            self.s = defaultChi
            self.damListOri = defaultDamList                
        self.m1,self.m2 = self.iniChi()
        self.damList = self.iniDamList()  
        
    def getMaxLen(self):
        dam_maxLen = 0
        for i in self.damList:
            if len(i)>dam_maxLen:
                dam_maxLen = len(i)
        return dam_maxLen
                
    def load(self):
        try:
            f = open('file.txt','rb')
            s = f.read()
            f.close()
            w = structWrapper(s)
            l = w.readWord()
            st = w.readArr(l).decode(encoding='utf8')
            self.s = st
            ll = []
            while w.leftLen()>0:
                l = w.readWord()
                ll.append(w.readArr(l).decode(encoding='utf8'))
            self.damListOri = ll                        
            return True
        except:
            return False
        
    def iniChi(self):
        m = {}
        m2 = {}
        s = self.s
        for i in range(len(s)):
            m[i] = s[i]
            m2 [s[i]] = i 
        return m,m2
    
    def iniDamList(self):
        l = []
        co = 0
        for st in self.damListOri:
            b = b''
            if len(st)%2==1:
                st += self.m1[0]                
            for i in range(len(st)):
                if i%2==0:
                    a = self.m2[st[i]]//16
                    b += bytes([a])
                    co = self.m2[st[i]]%16
                if i%2==1:
                    co = co*16+self.m2[st[i]]//256
                    b += bytes([co])
                    a = self.m2[st[i]]%256
                    b += bytes([a])  
            l.append(b)
        return l
  
    def b2chi(self,ss):
        st = ''
        ss += (3-len(ss)%3)*b'\x00'
        co = 0
        ll = []
        for i in range(len(ss)):
            if i%3==0:
                co = ss[i]*16
            elif i%3==1:
                co += ss[i]//16
                st += self.m1[co]
                co = (ss[i]%16)*256
            elif i%3==2:
                co += ss[i]
                st += self.m1[co]
        return st
if __name__ == '__main__':
    d = dam()
    print(d.s)
    print(d.damListOri)
    print(d.getMaxLen())